 package server;


import client.MapleClient;
import client.inventory.Item;
import client.inventory.ItemFlag;
import client.inventory.ItemLoader;
import client.inventory.MapleInventoryType;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import constants.ItemConstants;
import database.DatabaseConnection;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import tools.Pair;
import tools.packet.NPCPacket;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;























 public class MapleStorage implements Serializable {
/*  26 */   private static final Logger log = LogManager.getLogger(MapleStorage.class.getName());
   private static final long serialVersionUID = 9179541993413738569L;
   private final int storageId;
   private final int accountId;
   private final List<Item> items;
/*  31 */   private final Map<MapleInventoryType, List<Item>> typeItems = new EnumMap<>(MapleInventoryType.class);
   private Long meso;
   private short slots;
   private int storageNpcId;
   private boolean changed = false;

   @JsonCreator
   private MapleStorage(@JsonProperty("storageId") int storageId, @JsonProperty("solts") short slots, @JsonProperty("meso") Long meso, @JsonProperty("accountId") int accountId) {
/*  39 */     this.storageId = storageId;
/*  40 */     this.slots = slots;
/*  41 */     this.meso = meso;
/*  42 */     this.accountId = accountId;
/*  43 */     this.items = new LinkedList<>();
/*  44 */     if (this.slots > 128) {
/*  45 */       this.slots = 128;
/*  46 */       this.changed = true;
     }
   }




   public static int create(int accountId) throws SQLException {
/*  54 */     try(DruidPooledConnection con = DatabaseConnection.getInstance().getConnection(); 
/*  55 */         PreparedStatement ps = con.prepareStatement("INSERT INTO storages (accountid, slots, meso) VALUES (?, ?, ?)", 1)) {
/*  56 */       ps.setInt(1, accountId);
/*  57 */       ps.setInt(2, 4);
/*  58 */       ps.setInt(3, 0);
/*  59 */       ps.executeUpdate();


/*  62 */       try (ResultSet rs = ps.getGeneratedKeys()) {
/*  63 */         if (rs.next()) {
/*  64 */           int storageid = rs.getInt(1);
/*  65 */           ps.close();
/*  66 */           rs.close();
/*  67 */           return storageid;
         }
       }
     }

/*  72 */     throw new SQLException("Inserting char failed.");
   }




   public static MapleStorage loadOrCreateFromDB(int accountId) {
/*  79 */     MapleStorage ret = null;

/*  81 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/*  82 */       PreparedStatement ps = con.prepareStatement("SELECT * FROM storages WHERE accountid = ?");
/*  83 */       ps.setInt(1, accountId);
/*  84 */       ResultSet rs = ps.executeQuery();
/*  85 */       if (rs.next()) {
/*  86 */         int storeId = rs.getInt("storageid");
/*  87 */         ret = new MapleStorage(storeId, rs.getShort("slots"), Long.valueOf(rs.getLong("meso")), accountId);
/*  88 */         rs.close();
/*  89 */         ps.close();
/*  90 */         MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
/*  91 */         for (Pair<Item, MapleInventoryType> mit : (Iterable<Pair<Item, MapleInventoryType>>)ItemLoader.仓库道具.loadItems(false, accountId).values()) {
/*  92 */           Item item = (Item)mit.getLeft();
/*  93 */           if (item.getItemId() / 1000000 == 1 && ii.isDropRestricted(item.getItemId()) && !ItemFlag.可以交换1次.check(item.getFlag())) {
/*  94 */             item.addFlag((short)ItemFlag.可以交换1次.getValue());
           }
/*  96 */           ret.items.add(item);
         }
       } else {
/*  99 */         int storeId = create(accountId);
/* 100 */         ret = new MapleStorage(storeId, (short)4, Long.valueOf(0L), accountId);
/* 101 */         rs.close();
/* 102 */         ps.close();
       }
/* 104 */     } catch (SQLException ex) {
/* 105 */       System.err.println("Error loading storage" + ex);
     }
/* 107 */     return ret;
   }




   public void saveToDB() {
/* 114 */     saveToDB(null);
   }




   public void saveToDB(DruidPooledConnection con) {
/* 121 */     if (!this.changed) {
       return;
     }
/* 124 */     boolean needcolse = false;
     try {
/* 126 */       if (con == null) {
/* 127 */         con = DatabaseConnection.getInstance().getConnection();
       }
/* 129 */       PreparedStatement ps = con.prepareStatement("UPDATE storages SET slots = ?, meso = ? WHERE storageid = ?");
/* 130 */       ps.setInt(1, this.slots);
/* 131 */       ps.setLong(2, this.meso.longValue());
/* 132 */       ps.setInt(3, this.storageId);
/* 133 */       ps.executeUpdate();
/* 134 */       ps.close();

/* 136 */       List<Pair<Item, MapleInventoryType>> itemsWithType = new ArrayList<>();
/* 137 */       for (Item item : this.items) {
/* 138 */         itemsWithType.add(new Pair(item, ItemConstants.getInventoryType(item.getItemId())));
       }
/* 140 */       ItemLoader.仓库道具.saveItems(con, itemsWithType, this.accountId);
/* 141 */       this.changed = false;
/* 142 */     } catch (SQLException ex) {
/* 143 */       log.error("Error saving storage", ex);
     } finally {
/* 145 */       if (needcolse) {
         try {
/* 147 */           con.close();
/* 148 */         } catch (SQLException e) {
/* 149 */           log.error("Error saving storage", e);
         }
       }
     }
   }




   public Item getItem(short slot) {
/* 159 */     if (slot >= this.items.size() || slot < 0) {
/* 160 */       return null;
     }
/* 162 */     return this.items.get(slot);
   }




   public Item takeOut(short slot) {
/* 169 */     this.changed = true;
/* 170 */     Item ret = this.items.remove(slot);
/* 171 */     MapleInventoryType type = ItemConstants.getInventoryType(ret.getItemId());
/* 172 */     this.typeItems.put(type, new ArrayList<>(filterItems(type)));
/* 173 */     return ret;
   }




   public void store(Item item) {
/* 180 */     this.changed = true;
/* 181 */     this.items.add(item);
/* 182 */     MapleInventoryType type = ItemConstants.getInventoryType(item.getItemId());
/* 183 */     this.typeItems.put(type, new ArrayList<>(filterItems(type)));
   }




   public void arrange() {
/* 190 */     this.items.sort((o1, o2) -> (o1.getItemId() < o2.getItemId()) ? -1 : ((o1.getItemId() == o2.getItemId()) ? 0 : 1));








/* 199 */     for (MapleInventoryType type : MapleInventoryType.values()) {
/* 200 */       this.typeItems.put(type, new ArrayList<>(this.items));
     }
   }

   public List<Item> getItems() {
/* 205 */     return Collections.unmodifiableList(this.items);
   }

   private List<Item> filterItems(MapleInventoryType type) {
/* 209 */     List<Item> ret = new LinkedList<>();
/* 210 */     for (Item item : this.items) {
/* 211 */       if (ItemConstants.getInventoryType(item.getItemId()) == type) {
/* 212 */         ret.add(item);
       }
     }
/* 215 */     return ret;
   }

   public byte getSlot(MapleInventoryType type, byte slot) {
/* 219 */     byte ret = 0;
/* 220 */     List<Item> it = this.typeItems.get(type);
/* 221 */     if (it == null || slot >= it.size() || slot < 0) {
/* 222 */       return -1;
     }
/* 224 */     for (Item item : this.items) {
/* 225 */       if (item == it.get(slot)) {
/* 226 */         return ret;
       }
/* 228 */       ret = (byte)(ret + 1);
     }
/* 230 */     return -1;
   }

   public void sendStorage(MapleClient c, int npcId) {
/* 234 */     this.storageNpcId = npcId;
/* 235 */     this.items.sort((o1, o2) -> (ItemConstants.getInventoryType(o1.getItemId()).getType() < ItemConstants.getInventoryType(o2.getItemId()).getType()) ? -1 : ((ItemConstants.getInventoryType(o1.getItemId()) == ItemConstants.getInventoryType(o2.getItemId())) ? 0 : 1));








/* 244 */     for (MapleInventoryType type : MapleInventoryType.values()) {
/* 245 */       this.typeItems.put(type, new ArrayList<>(this.items));
     }
/* 247 */     c.announce(NPCPacket.getStorage(npcId, this.slots, this.items, this.meso.longValue()));
   }

   public void update(MapleClient c) {
/* 251 */     c.announce(NPCPacket.arrangeStorage(this.slots, this.items, true));
   }

   public void sendStored(MapleClient c, MapleInventoryType type) {
/* 255 */     c.announce(NPCPacket.storeStorage(this.slots, type, this.typeItems.get(type)));
   }

   public void sendTakenOut(MapleClient c, MapleInventoryType type) {
/* 259 */     c.announce(NPCPacket.takeOutStorage(this.slots, type, this.typeItems.get(type)));
   }

   public long getMeso() {
/* 263 */     return this.meso.longValue();
   }

   public void setMeso(long meso) {
/* 267 */     if (meso < 0L) {
       return;
     }
/* 270 */     this.changed = true;
/* 271 */     this.meso = Long.valueOf(meso);
   }




   public Item findById(int itemId) {
/* 278 */     for (Item item : this.items) {
/* 279 */       if (item.getItemId() == itemId) {
/* 280 */         return item;
       }
     }
/* 283 */     return null;
   }

   public void sendMeso(MapleClient c) {
/* 287 */     c.announce(NPCPacket.mesoStorage(this.slots, this.meso.longValue()));
   }

   public boolean isFull() {
/* 291 */     return (this.items.size() >= this.slots);
   }

   public int getSlots() {
/* 295 */     return this.slots;
   }

   public void setSlots(byte set) {
/* 299 */     this.changed = true;
/* 300 */     this.slots = (short)set;
   }

   public void increaseSlots(byte gain) {
/* 304 */     this.changed = true;
/* 305 */     this.slots = (short)(this.slots + gain);
   }

   public int getNpcId() {
/* 309 */     return this.storageNpcId;
   }

   public void close() {
/* 313 */     this.typeItems.clear();
   }
 }


/* Location:              C:\PGX\Server.jar!\server\MapleStorage.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */